//
//  YLMallGoodsExchangeBuyView.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/6.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLMallGoodsExchangeBuyView.h"
#import <Masonry.h>
#import "YLGlobeHeader.h"
@implementation YLMallGoodsExchangeBuyView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _closeSubject = [RACSubject subject];
        [self setUpChoiceNumber];
    }
    return self;
}


- (void)setUpChoiceNumber
{
    __weak typeof(self) weakSelf = self;
    self.backgroundColor = YLColor(248, 249, 252);
   
    UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeButton setImage:[UIImage imageNamed:@"closeOrg"] forState:UIControlStateNormal];
    [self addSubview:closeButton];
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf).mas_offset(10);
        make.left.mas_equalTo(weakSelf).mas_offset(20);
        make.width.height.mas_equalTo(20);
    }];
    
    [[closeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf.closeSubject sendNext:nil];
    }];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"兑换数量";
    titleLabel.textColor = YLLineColor;
    [weakSelf addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf);
        make.top.mas_equalTo(weakSelf).mas_offset(20);
    }];
    [titleLabel sizeToFit];
    
    UITextField *numField = [[UITextField alloc] init];
    numField.keyboardType =UIKeyboardTypeNumberPad;
    numField.textAlignment = NSTextAlignmentCenter;
    numField.userInteractionEnabled = YES;
    numField.backgroundColor = [UIColor whiteColor];
    [weakSelf addSubview:numField];
    
    RAC(self, number) = RACObserve(numField, text);
    numField.text = @"1";
    [numField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf).mas_offset(50);
        make.height.mas_equalTo(20);
        make.centerX.mas_equalTo(weakSelf);
        make.width.mas_equalTo(60);
    }];
    
    UIButton *subButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:subButton];
    [subButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(numField);
        make.width.mas_equalTo(numField.mas_height);
        make.right.mas_equalTo(numField.mas_left).mas_offset(-5);
    }];
    [subButton setTitle:@"-" forState:UIControlStateNormal];
    subButton.titleLabel.font = [UIFont systemFontOfSize:30.0f];
    [subButton setTitleColor:YLLineColor forState:UIControlStateNormal];
    [[subButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSInteger i =  [numField.text integerValue];
        if (i < 1) {
            i = 0;
        } else {
            i--;
        }
        numField.text = [NSString stringWithFormat:@"%zd", i];
    }];
    
    
    
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [[addButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSInteger i =  [numField.text integerValue];
        i ++;
        numField.text = [NSString stringWithFormat:@"%zd", i];
    }];
    [self addSubview:addButton];
    [addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(numField);
        make.width.mas_equalTo(numField.mas_height);
        make.left.mas_equalTo(numField.mas_right).mas_offset(5);
    }];
    [addButton setTitle:@"+" forState:UIControlStateNormal];
    addButton.titleLabel.font = [UIFont systemFontOfSize:30.0f];
    [addButton setTitleColor:YLLOrgColor forState:UIControlStateNormal];
    
    
}


@end
